package br.unb.midialab.pianogame.model;

import br.unb.midialab.pianogame.main.JogoPiano;

public class Nota {
	String nome;
	int midiKey;
	int octave;
	int keyVelocity;
	long noteOnTick;
	long noteOffTick;
	long noteDuration;
	float notePXWidth = 40;
	int pxGridLine = 1;
	float notePXHeight;
	int posX;
	int posY;
	JogoPiano parent;
	float offSet = 0.8f; // pixels Per tick
	int r, g, b;

	public long getNoteDuration() {
		return noteDuration;
	}

	public void setNoteDuration(long noteDuration) {
		this.noteDuration = noteDuration;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public int getMidiKey() {
		return midiKey;
	}

	public void setMidiKey(int midiKey) {
		this.midiKey = midiKey;
	}

	public int getOctave() {
		return octave;
	}

	public void setOctave(int octave) {
		this.octave = octave;
	}

	public int getKeyVelocity() {
		return keyVelocity;
	}

	public void setKeyVelocity(int keyVelocity) {
		this.keyVelocity = keyVelocity;
	}

	public long getNoteOnTick() {
		return noteOnTick;
	}

	public void setNoteOnTick(long noteOnTick) {
		this.noteOnTick = (noteOnTick);
	}

	public long getNoteOffTick() {
		return noteOffTick;
	}

	public void setNoteOffTick(long noteOffTick) {
		this.noteOffTick = (noteOffTick);
		this.noteDuration = noteOffTick - noteOnTick;
		this.notePXHeight = noteDuration * offSet;
	}

	public int getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}

	public int getPosY() {
		return posY;
	}

	public void setParent(JogoPiano parent) {
		this.parent = parent;
		setPosX((int) (midiKey - 60) * 40 + (midiKey - 60));
		r = (int) parent.random(255);
		g = (int) parent.random(255);
		b = (int) parent.random(255);
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public void draw() {

		parent.fill(r, g, b,keyVelocity*2);
		if (getPosY() > 0 && (getPosY() + notePXHeight) < parent.height) {

			parent.rect(posX, posY, notePXWidth, notePXHeight);
			parent.fill(255);
			parent.text(nome, posX+notePXWidth/2, posY+notePXHeight/2);

		}
	}

	public void update(long tick) {
		//Seta e remove as notas correntes na lista de notas correntes, com um certo tempo de erro de 1 tick a mais ou a menos.
		/**/
		if ((noteOnTick + tick) >= -1 && (noteOnTick + tick) <= 1 ) {
			parent.setCurrentNoteList(this);
		}

		if (noteOffTick + tick == 0) {
			parent.removeFromCurrentNoteList(this);
		}
		// atualizar a posX baseado no tick atual e no tick de inicio da nota.
		int tamPiano = 128;
		posY = ((int) (parent.height - (tamPiano - (noteOnTick + tick) * offSet)));
//		if (noteOnTick + tick == 0) {
//			parent.noteOn(midiKey, keyVelocity);
//		}
//
//		if (noteOffTick + tick == 0) {
//			parent.noteOff(midiKey);
//		}

	}
}
